package com.example.demo.scheduledtask;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.demo.entity.*;
import com.example.demo.mapper.*;
import com.example.demo.utils.RestTemplateUtil;
import com.example.demo.vo.CheckmanAndWpno;
import com.example.demo.vo.DeviceUserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 *
 */
@Component
@Slf4j
public class DeviceDataScheduledTask {

//    @Autowired
//    private DeviceMapper deviceMapper;
//
//    @Autowired
//    private WorkOrderListMapper workOrderListMapper;
//
//    @Autowired
//    private DispatchOrderMapper dispatchOrderMapper;
//
//    @Autowired
//    private QcCheckmaterialMapper qcCheckmaterialMapper;
//
//    @Autowired
//    private QcNumericMapper qcNumericMapper;
//
//    @Autowired
//    private QcCheckmaterialListMapper qcCheckmaterialListMapper;
//
//    @Autowired
//    private RestTemplateUtil restTemplateUtil;
//
//    @Autowired
//    private DeviceStopStatusScheduledTask deviceStopStatusScheduledTask;
//
//
//    public void deviceDataScheduledTask() {
//        try {
//            ////定时同步设备一车间开机的抽真空设备数据，抽真空设备暂时只支持ZKJ-LB：真空机（莱宝D40C）.split("_")
//            //this.syncAMA13001ZKJLBDataSplitBy_();
//            //定时同步设备一车间开机的抽真空设备数据，抽真空设备暂时只支持ZKJ-LB：真空机（莱宝D40C）.splitByMap
//            //log.info("定时同步设备设备状态");
//            deviceStopStatusScheduledTask.deviceStopStatusScheduledTask();
//        } catch (Exception e) {
//            log.error("定时同步设备设备状态出现异常：{}", e);
//        }
//
//        try {
//            ////定时同步设备一车间开机的抽真空设备数据，抽真空设备暂时只支持ZKJ-LB：真空机（莱宝D40C）.split("_")
//            //this.syncAMA13001ZKJLBDataSplitBy_();
//            //定时同步设备一车间开机的抽真空设备数据，抽真空设备暂时只支持ZKJ-LB：真空机（莱宝D40C）.splitByMap
//            //log.info("定时同步设备一车间开机的抽真空设备数据");
//            this.syncAMA13001ZKJLBDataSplitByMap();
//        } catch (Exception e) {
//            log.error("定时同步设备一车间开机的抽真空设备数据出现异常：{}", e);
//        }
//
//        //定时同步设备1，2，4车间的测试机,CSTWJ_HK 间测试台位机（弘科自动化）
//        try {
//            //log.info("定时同步设备1，2，4车间的测试机");
//            this.syncCSTWJHKData();
//        } catch (Exception e) {
//            log.error("定时同步设备1，2，4车间的测试机");
//        }
//
//    }
//
//    /**
//     * 设备点检
//     */
//    @Scheduled(cron = "0 0/1 * * * ?")
//    @Async(value = "taskExecutor")
//    public void deviceInspectionScheduledTask() {
//        // 创建一个Calendar对象
//        Calendar calendar = Calendar.getInstance();
//
//        // 获取当前日期的小时和分钟
//        Integer hour = calendar.get(Calendar.HOUR_OF_DAY);
//        Integer minute = calendar.get(Calendar.MINUTE);
//        log.info("{}:{}",hour, minute);
//        ////log.info("定时发送设备点检信息");
//        //先获取车间设备点检通知时间
//        List<String> wsnos = deviceMapper.getWsnosByTime(hour, minute);
//        //log.info("车间：",wsnos.toString());
//        if(CollectionUtils.isNotEmpty(wsnos)){
//            List<DeviceUserVo> deviceUserVos = deviceMapper.selectDeviceUserVos(wsnos);
//            if (CollectionUtils.isNotEmpty(deviceUserVos)) {
//                for (DeviceUserVo deviceUserVo : deviceUserVos) {
//                    deviceMapper.pushMessage(deviceUserVo.getLinkphone(), deviceUserVo.getDevicename() + "，设备"+deviceUserVo.getDeviceno() + "需要点检");
//                    log.info("设备点检消息通知：" + deviceUserVo.getDevicename() + "，设备"+deviceUserVo.getDeviceno() + "需要点检");
//                }
//            }
//        }
//    }
//
//    /**
//     * 定时同步设备1，2，4车间的测试机,CSTWJ_HK 间测试台位机（弘科自动化）
//     */
//    @Scheduled(cron = "0/3 * * * * ?")
//    @Async(value = "taskExecutor")
//    public void syncCSTWJHKData() {
//        //log.info("定时同步设备1，2，4车间的测试机");
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
//        SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
//        //2023-11-05 12:50:24.000000
//        SimpleDateFormat dateFormat3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
//        LambdaQueryWrapper<Device> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(Device::getDetypeno, "CSTWJ_HK");
//        //.eq(Device::getRunstates, 0);
//        List<Device> devices = deviceMapper.selectList(queryWrapper);
//        ////log.info(devices.toString());
//        if (CollectionUtils.isNotEmpty(devices)) {
//            for (Device device : devices) {
//                Map<String, Object> params = new HashMap<>();
//                params.put("deviceId", device.getDeviceno());
//                ////log.info("同步设备1，2，4车间的测试机数据 params: {}", params);
//                try {
//                    JSONObject jsonObject = restTemplateUtil.get("http://172.19.255.89:8083/zwz/deviceDataNew/getDeviceDataByDeviceBarIdInRecentDayOnScreen", params);
//                    //////log.info("response: {}",jsonObject);
//                    ////log.info(jsonObject.toJSONString());
//                    if (jsonObject.getBoolean("success")) {
//                        List<String> result = jsonObject.getObject("result", List.class);
//                        //{
//                        //    "result": [
//                        //        "2023-10-26 16:59:36.000000##@@-1##@@{\"isCleaned\":\"false\", \"deviceId\":\"AMASCO397\", \"guid\":\"null\", \"testTime\":\"2023-10-26 16:59:36.000000\", \"barcode\":\"10300800200123-231023-0045\", \"modelId\":\"null\", \"model\":\"df18kw\", \"testNo\":\"8\", \"result\":\"null\", \"machine\":\"0\", \"stepIndex\":\"0\", \"dataResult\":\"0\", \"step\":\"1\", \"dataValues0\":\"14.8809\", \"dataValues1\":\"0.0\", \"dataValues2\":\"0.0\", \"dataValues3\":\"0.0\", \"dataValues4\":\"0.0\", \"dataValues5\":\"0.0\", \"dataValues6\":\"0.0\", \"dataValues7\":\"0.0\", \"dataValues8\":\"0.0\", \"dataValues9\":\"0.0\", \"dataValues10\":\"0.0\", \"dataValues11\":\"0.0\", \"dataValues12\":\"0.0\", \"dataValues13\":\"0.0\", \"dataValues14\":\"0.0\", \"dataValues15\":\"0.0\", \"dataValues16\":\"0.0\", \"dataValues17\":\"0.0\", \"dataValues18\":\"0.0\", \"dataValues19\":\"0.0\", \"dataResults0\":\"1\", \"dataResults1\":\"0\", \"dataResults2\":\"0\", \"dataResults3\":\"0\", \"dataResults4\":\"0\", \"dataResults5\":\"0\", \"dataResults6\":\"0\", \"dataResults7\":\"0\", \"dataResults8\":\"0\", \"dataResults9\":\"0\", \"dataResults10\":\"0\", \"dataResults11\":\"0\", \"dataResults12\":\"0\", \"dataResults13\":\"0\", \"dataResults14\":\"0\", \"dataResults15\":\"0\", \"dataResults16\":\"0\", \"dataResults17\":\"0\", \"dataResults18\":\"0\", \"dataResults19\":\"0\"}"
//                        //    ],
//                        //    "success": true,
//                        //时间##@@条码（空为-1##@@_id_isCleaned_guid_testTime_barcode_modelId_testNo_result_machine_stepIndex_dataResult_step_dataValues0
//                        //2023-10-30 16:54:04##@@10300800200093-231023-0065##@@6539e1684cd6ca72d7c27b38_FALSE_{fd5a16ec-350d-4516-9fc9-c0697550f965}_2023/10/26 11:43:50_10300800200093-231023-0065_10300800200093_3_1_0_0_0_1_9.94643_0_0
//                        //2023-10-30 16:54:04##@@-1##@@6539e1684cd6ca72d7c27b38_FALSE_{fd5a16ec-350d-4516-9fc9-c0697550f965}_2023/10/26 11:43:50_-1_3_1_0_0_0_1_9.94643_0_0
//                        //			                测试时间	     条码	设备号	结果1合格，2不合格		步骤号，顺序	结果0合格，1不合格	步骤1：开始，2准备	值，只有这个有数值，后续都是空
//                        //_id	isCleaned	guid	testTime	barcode	modelId	testNo	result	machine	stepIndex	dataResult	step	dataValues0
//                        if (result.size() > 0) {
//                            String[] splitArray1 = result.get(0).split("##@@");
//                            if (splitArray1.length >= 3) {
//                                ////log.info(splitArray1[2]);
//                                Map<String, String> map = com.alibaba.fastjson.JSONObject.parseObject(splitArray1[2], Map.class);
//                                if ("-1".equals(splitArray1[1])) {
//                                    //log.error("该设备{}无带条码数据",device.getDeviceno());
//                                    continue;
//                                }
//                                //"AFQ202300000448"
//                                //INSERT INTO qc_checkmaterial (checkno,mcode,checkman,mkerid,ddate,mkerdate,billtyp,srcbillno,istates,wpno,linesno,tm,chkresult,dno,value,deviceno,numericdatatime)
//                                //VALUES (@billno,@mcode,@userno,@userno,getdate(),getdate(),@xjtype,@pgd,2,@gw,@linesno,@pc,@checkresult,@dno,@result,@deviceno,cast(@time as datetime))
//                                String tm = map.get("bar"); //tm
//                                if (tm.indexOf("-") < 0) {
//                                    //10300800100018 231102 0418
//                                    ////log.info("条码：{}", tm);
//                                    tm = tm.substring(0, 14) + "-" + tm.substring(14, 20) + "-" + tm.substring(20, tm.length());
//                                }
//                                LambdaQueryWrapper<WorkOrderList> workOrderListLambdaQueryWrapper = new LambdaQueryWrapper<>();
//                                workOrderListLambdaQueryWrapper.eq(WorkOrderList::getBarcode, tm);
//                                List<WorkOrderList> workOrderLists = workOrderListMapper.selectList(workOrderListLambdaQueryWrapper);
//                                if (CollectionUtils.isNotEmpty(workOrderLists)) {
//                                    Integer chkresult = 1; //chkresult 0.合格	0  1.不合格	1
//                                    if ("True".equalsIgnoreCase(map.get("isPass"))) {
//                                        chkresult = 0;
//                                    }
//                                    //判断数据是否已处理，已处理就不保存
//                                    LambdaQueryWrapper<QcNumeric> qcNumericQueryWrapper = new LambdaQueryWrapper<>();
//                                    qcNumericQueryWrapper.eq(QcNumeric::getTm, tm)
//                                            .eq(QcNumeric::getDeviceno, device.getDeviceno())
//                                            .eq(QcNumeric::getChkresult, chkresult);
//                                    Long count = qcNumericMapper.selectCount(qcNumericQueryWrapper);
//                                    if (count > 0) {
//                                        log.debug("数据已处理");
//                                        continue;
//                                    }
//                                    WorkOrderList workOrderList = workOrderLists.get(0);
//                                    LambdaQueryWrapper<DispatchOrder> dispatchOrderLambdaQueryWrapper = new LambdaQueryWrapper<>();
//                                    dispatchOrderLambdaQueryWrapper.eq(DispatchOrder::getBillno, workOrderList.getWorkOrder());
//                                    List<DispatchOrder> dispatchOrders = dispatchOrderMapper.selectList(dispatchOrderLambdaQueryWrapper);
//                                    if (CollectionUtils.isNotEmpty(dispatchOrders)) {
//                                        DispatchOrder dispatchOrder = dispatchOrders.get(0);
//                                        String uuid = UUID.randomUUID().toString();
//                                        String checkno = new String("AFQ" + dateFormat.format(new Date()) + uuid.substring(uuid.length() - 4, uuid.length()));
//                                        String mcode = dispatchOrder.getMcode();
//                                        CheckmanAndWpno checkmanAndWpno = deviceMapper.selectCheckmanAndWpno(dispatchOrder.getBillno(), "JOB00122");
//                                        String checkman = "10528";
//                                        String mkerid = "10528";
//                                        String wpno = "CX290_047"; //gw
//                                        ////log.info("checkmanAndWpno: " + checkmanAndWpno);
//                                        if (ObjectUtils.isNotEmpty(checkmanAndWpno)) {
//                                            checkman = checkmanAndWpno.getEmpno();
//                                            mkerid = checkmanAndWpno.getEmpno();
//                                            wpno = checkmanAndWpno.getWpno(); //gw
//                                        }
//                                        Date ddate = null;
//                                        try {
//                                            ddate = dateFormat3.parse(map.get("testTime"));
//                                            ////log.info(ddate.toString());
//                                        } catch (ParseException e) {
//                                            throw new RuntimeException(e);
//                                        }
//                                        Date mkerdate = ddate;
//                                        //1.来料检验	1  2.首检检验	2  3.成品检验	3  4.巡检检验	4  5.送检检验	5  99.条码冻结	99  出库检验	10  专检	11  12数采（抽真空（泰斯特））	12  121数采（冷媒灌注机）	121  123真空机（莱宝）	123  122安全性能测试仪	122  13品质设备点检	13  14卤检	14  15外观	15
//                                        Integer billtyp = 124; //间测试台位机（弘科自动化）
//                                        String srcbillno = dispatchOrder.getBillno(); //pgd
//                                        Integer istates = 2; //2
//                                        String linesno = dispatchOrder.getLinesno(); //linesno
//                                        //String tm = splitArray2[4]; //tm
//                                        String deviceno = device.getDeviceno(); //设备编号
//                                        QcNumeric qcNumeric = new QcNumeric();
//                                        qcNumeric.setCheckno(checkno);
//                                        qcNumeric.setMcode(mcode);
//                                        qcNumeric.setCheckman(checkman);
//                                        qcNumeric.setMkerid(mkerid);
//                                        qcNumeric.setDdate(ddate);
//                                        qcNumeric.setMkerdate(mkerdate);
//                                        qcNumeric.setBilltyp(billtyp);
//                                        qcNumeric.setSrcbillno(srcbillno);
//                                        qcNumeric.setIstates(istates);
//                                        qcNumeric.setWpno(wpno);
//                                        // qcNumeric.setLinesno(linesno);
//                                        qcNumeric.setTm(tm);
//                                        qcNumeric.setBar(tm);
//                                        qcNumeric.setChkresult(chkresult);
//                                        qcNumeric.setDeviceno(deviceno);
//                                        qcNumericMapper.insert(qcNumeric);
//
//                                        QcNumericlist qcNumericlist = new QcNumericlist();
//                                        qcNumericlist.setCheckno(checkno);
//                                        qcNumericlist.setBar(tm);
//                                        qcNumericlist.setChkresult(chkresult);
//                                        qcNumericlist.setAutoid(1);
//                                        qcNumericlist.setDeviceno(deviceno);
//                                        qcNumericlist.setMode(map.get("mode"));
//                                        qcNumericlist.setStep(map.get("step"));
//                                        qcNumericlist.setTestno(map.get("testNo"));
//                                        qcNumericlist.setTestTime(ddate);
//                                        qcNumericlist.setStepId(map.get("stepId"));
//                                        qcNumericlist.setD0(new BigDecimal(map.get("d0")));
//                                        qcNumericlist.setD1(new BigDecimal(map.get("d1")));
//                                        qcNumericlist.setD2(new BigDecimal(map.get("d2")));
//                                        qcNumericlist.setD3(new BigDecimal(map.get("d3")));
//                                        qcNumericlist.setD4(new BigDecimal(map.get("d4")));
//                                        qcNumericlist.setD5(new BigDecimal(map.get("d5")));
//                                        qcNumericlist.setD6(new BigDecimal(map.get("d6")));
//                                        qcNumericlist.setD7(new BigDecimal(map.get("d7")));
//                                        qcNumericlist.setD8(new BigDecimal(map.get("d8")));
//                                        qcNumericlist.setD9(new BigDecimal(map.get("d9")));
//                                        qcNumericlist.setD10(new BigDecimal(map.get("d10")));
//                                        qcNumericlist.setD11(new BigDecimal(map.get("d11")));
//                                        qcNumericlist.setD12(new BigDecimal(map.get("d12")));
//                                        qcNumericlist.setD13(new BigDecimal(map.get("d13")));
//                                        qcNumericlist.setD14(new BigDecimal(map.get("d14")));
//                                        qcNumericlist.setD15(new BigDecimal(map.get("d15")));
//                                        qcNumericlist.setD16(new BigDecimal(map.get("d16")));
//                                        qcNumericlist.setD17(new BigDecimal(map.get("d17")));
//                                        qcNumericlist.setD18(new BigDecimal(map.get("d18")));
//                                        qcNumericlist.setD19(new BigDecimal(map.get("d19")));
//                                        qcNumericlist.setD20(new BigDecimal(map.get("d20")));
//                                        qcNumericlist.setD21(new BigDecimal(map.get("d21")));
//                                        qcNumericlist.setD22(new BigDecimal(map.get("d22")));
//                                        qcNumericlist.setD23(new BigDecimal(map.get("d23")));
//                                        qcNumericlist.setD24(new BigDecimal(map.get("d24")));
//                                        qcNumericlist.setD25(new BigDecimal(map.get("d25")));
//                                        qcNumericlist.setD26(new BigDecimal(map.get("d26")));
//                                        qcNumericlist.setD27(new BigDecimal(map.get("d27")));
//                                        qcNumericlist.setD28(new BigDecimal(map.get("d28")));
//                                        qcNumericlist.setD29(new BigDecimal(map.get("d29")));
//                                        qcNumericlist.setD30(new BigDecimal(map.get("d30")));
//                                        qcNumericlist.setD31(new BigDecimal(map.get("d31")));
//                                        qcNumericlist.setD32(new BigDecimal(map.get("d32")));
//                                        qcNumericlist.setD33(new BigDecimal(map.get("d33")));
//                                        qcNumericlist.setD34(new BigDecimal(map.get("d34")));
//                                        qcNumericlist.setD35(new BigDecimal(map.get("d35")));
//                                        qcNumericlist.setD36(new BigDecimal(map.get("d36")));
//                                        qcNumericlist.setD37(new BigDecimal(map.get("d37")));
//                                        qcNumericlist.setD38(new BigDecimal(map.get("d38")));
//                                        qcNumericlist.setD39(new BigDecimal(map.get("d39")));
//                                        qcNumericlist.setD40(new BigDecimal(map.get("d40")));
//                                        qcNumericlist.setD41(new BigDecimal(map.get("d41")));
//                                        qcNumericlist.setD42(new BigDecimal(map.get("d42")));
//                                        qcNumericlist.setD43(new BigDecimal(map.get("d43")));
//                                        qcNumericlist.setD44(new BigDecimal(map.get("d44")));
//                                        qcNumericlist.setD45(new BigDecimal(map.get("d45")));
//                                        qcNumericlist.setD46(new BigDecimal(map.get("d46")));
//                                        qcNumericlist.setD47(new BigDecimal(map.get("d47")));
//                                        qcNumericlist.setD48(new BigDecimal(map.get("d48")));
//                                        qcNumericlist.setD49(new BigDecimal(map.get("d49")));
//                                        try {
//                                            qcNumericlist.setD50(new BigDecimal(map.get("d50")));
//                                            qcNumericlist.setD51(new BigDecimal(map.get("d51")));
//                                            qcNumericlist.setD52(new BigDecimal(map.get("d52")));
//                                            qcNumericlist.setD53(new BigDecimal(map.get("d53")));
//                                            qcNumericlist.setD54(new BigDecimal(map.get("d54")));
//                                            qcNumericlist.setD55(new BigDecimal(map.get("d55")));
//                                            qcNumericlist.setD56(new BigDecimal(map.get("d56")));
//                                            qcNumericlist.setD57(new BigDecimal(map.get("d57")));
//                                            qcNumericlist.setD58(new BigDecimal(map.get("d58")));
//                                        } catch (Exception e) {
//                                            log.error("d50后无数据");
//                                        }
//                                        qcCheckmaterialListMapper.insert(qcNumericlist);
//                                        ////log.info("导入测试台位机数据成功");
//                                        log.info("同步测试台位机数据成功，设备{}，tm:{}", deviceno, tm);
//                                    } else {
//                                        log.error("无效派工单：{}", workOrderList.getWorkOrder());
//                                    }
//                                } else {
//                                    //log.error("无效条码：{}", tm);
//                                }
//                            } else {
//                                log.error("数据拆分异常：{}", result.get(0));
//                            }
//                        } else {
//                            //log.error("未获取到数据", result);
//                        }
//                    } else {
//                        //log.error("获取数据失败：{}", jsonObject);
//                    }
//                } catch (IOException e) {
//                    throw new RuntimeException(e);
//                }
//            }
//        } else {
//            log.error("未找到启动的设备！");
//        }
//    }
//
//    /**
//     * 定时同步设备一车间开机的抽真空设备数据，抽真空设备暂时只支持ZKJ-LB：真空机（莱宝D40C）.splitByMap
//     */
//    @Scheduled(cron = "0/1 * * * * ?")
//    @Async(value = "taskExecutor")
//    public void syncAMA13001ZKJLBDataSplitByMap() {
//        //log.info("定时同步设备一车间开机的抽真空设备数据");
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
//        SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
//        LambdaQueryWrapper<Device> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(Device::getDptno, "AMA13001")
//                .eq(Device::getDetypeno, "ZKJ-LB")
//                .eq(Device::getRunstates, 0);
//        List<Device> devices = deviceMapper.selectList(queryWrapper);
//        ////log.info(devices.toString());
//        if (CollectionUtils.isNotEmpty(devices)) {
//            for (Device device : devices) {
//                Map<String, Object> params = new HashMap<>();
//                params.put("deviceId", device.getDeviceno());
//                ////log.info("获取抽真空设备数据 params: {}", params);
//                try {
//                    JSONObject jsonObject = restTemplateUtil.get("http://172.19.255.89:8083/zwz/deviceDataNew/getDeviceDataByDeviceBarIdInRecentDayOnScreen", params);
//                    //////log.info("response: {}",jsonObject);
//                    ////log.info(jsonObject.toJSONString());
//                    if (jsonObject.getBoolean("success")) {
//                        List<String> result = jsonObject.getObject("result", List.class);
//
//                        //{
//                        //    "result": [
//                        //        "2023-10-26 16:59:36.000000##@@-1##@@{\"isCleaned\":\"false\", \"deviceId\":\"AMASCO397\", \"guid\":\"null\", \"testTime\":\"2023-10-26 16:59:36.000000\", \"barcode\":\"10300800200123-231023-0045\", \"modelId\":\"null\", \"model\":\"df18kw\", \"testNo\":\"8\", \"result\":\"null\", \"machine\":\"0\", \"stepIndex\":\"0\", \"dataResult\":\"0\", \"step\":\"1\", \"dataValues0\":\"14.8809\", \"dataValues1\":\"0.0\", \"dataValues2\":\"0.0\", \"dataValues3\":\"0.0\", \"dataValues4\":\"0.0\", \"dataValues5\":\"0.0\", \"dataValues6\":\"0.0\", \"dataValues7\":\"0.0\", \"dataValues8\":\"0.0\", \"dataValues9\":\"0.0\", \"dataValues10\":\"0.0\", \"dataValues11\":\"0.0\", \"dataValues12\":\"0.0\", \"dataValues13\":\"0.0\", \"dataValues14\":\"0.0\", \"dataValues15\":\"0.0\", \"dataValues16\":\"0.0\", \"dataValues17\":\"0.0\", \"dataValues18\":\"0.0\", \"dataValues19\":\"0.0\", \"dataResults0\":\"1\", \"dataResults1\":\"0\", \"dataResults2\":\"0\", \"dataResults3\":\"0\", \"dataResults4\":\"0\", \"dataResults5\":\"0\", \"dataResults6\":\"0\", \"dataResults7\":\"0\", \"dataResults8\":\"0\", \"dataResults9\":\"0\", \"dataResults10\":\"0\", \"dataResults11\":\"0\", \"dataResults12\":\"0\", \"dataResults13\":\"0\", \"dataResults14\":\"0\", \"dataResults15\":\"0\", \"dataResults16\":\"0\", \"dataResults17\":\"0\", \"dataResults18\":\"0\", \"dataResults19\":\"0\"}"
//                        //    ],
//                        //    "success": true,
//                        //时间##@@条码（空为-1##@@_id_isCleaned_guid_testTime_barcode_modelId_testNo_result_machine_stepIndex_dataResult_step_dataValues0
//                        //2023-10-30 16:54:04##@@10300800200093-231023-0065##@@6539e1684cd6ca72d7c27b38_FALSE_{fd5a16ec-350d-4516-9fc9-c0697550f965}_2023/10/26 11:43:50_10300800200093-231023-0065_10300800200093_3_1_0_0_0_1_9.94643_0_0
//                        //2023-10-30 16:54:04##@@-1##@@6539e1684cd6ca72d7c27b38_FALSE_{fd5a16ec-350d-4516-9fc9-c0697550f965}_2023/10/26 11:43:50_-1_3_1_0_0_0_1_9.94643_0_0
//                        //			                测试时间	     条码	设备号	结果1合格，2不合格		步骤号，顺序	结果0合格，1不合格	步骤1：开始，2准备	值，只有这个有数值，后续都是空
//                        //_id	isCleaned	guid	testTime	barcode	modelId	testNo	result	machine	stepIndex	dataResult	step	dataValues0
//                        if (result.size() > 0) {
//                            String[] splitArray1 = result.get(0).split("##@@");
//                            if (splitArray1.length >= 3) {
//                                ////log.info(splitArray1[2]);
//                                Map<String, String> map = com.alibaba.fastjson.JSONObject.parseObject(splitArray1[2], Map.class);
//                                if ("-1".equals(splitArray1[1])) {
//                                    // log.error("该设备{}无带条码数据",device.getDeviceno());
//                                    continue;
//                                }
//                                //"AFQ202300000448"
//                                //INSERT INTO qc_checkmaterial (checkno,mcode,checkman,mkerid,ddate,mkerdate,billtyp,srcbillno,istates,wpno,linesno,tm,chkresult,dno,value,deviceno,numericdatatime)
//                                //VALUES (@billno,@mcode,@userno,@userno,getdate(),getdate(),@xjtype,@pgd,2,@gw,@linesno,@pc,@checkresult,@dno,@result,@deviceno,cast(@time as datetime))
//                                String tm = map.get("barcode"); //tm
//                                if (tm.indexOf("-") < 0) {
//                                    //10300800100018 231102 0418
//                                    ////log.info("条码：{}", tm);
//                                    tm = tm.substring(0, 14) + "-" + tm.substring(14, 20) + "-" + tm.substring(20, tm.length());
//                                }
//                                ////log.info("条码：{}", tm);
//                                LambdaQueryWrapper<WorkOrderList> workOrderListLambdaQueryWrapper = new LambdaQueryWrapper<>();
//                                workOrderListLambdaQueryWrapper.eq(WorkOrderList::getBarcode, tm);
//                                List<WorkOrderList> workOrderLists = workOrderListMapper.selectList(workOrderListLambdaQueryWrapper);
//                                if (CollectionUtils.isNotEmpty(workOrderLists)) {
//                                    Integer chkresult = Integer.parseInt(map.get("dataResult")); //chkresult 0.合格	0  1.不合格	1
//                                    String stepIndex = map.get("stepIndex");
//                                    //判断数据是否已处理，已处理就不保存
//                                    LambdaQueryWrapper<QcCheckmaterial> qcCheckmaterialLambdaQueryWrapper = new LambdaQueryWrapper<>();
//                                    qcCheckmaterialLambdaQueryWrapper.eq(QcCheckmaterial::getTm, tm)
//                                            .eq(QcCheckmaterial::getDeviceno, device.getDeviceno())
//                                            .eq(QcCheckmaterial::getChkresult, chkresult)
//                                            .eq(QcCheckmaterial::getRemark1, stepIndex);
//                                    Long count = qcCheckmaterialMapper.selectCount(qcCheckmaterialLambdaQueryWrapper);
//                                    if (count > 0) {
//                                        ////log.info("数据已处理");
//                                        continue;
//                                    }
//                                    WorkOrderList workOrderList = workOrderLists.get(0);
//                                    LambdaQueryWrapper<DispatchOrder> dispatchOrderLambdaQueryWrapper = new LambdaQueryWrapper<>();
//                                    dispatchOrderLambdaQueryWrapper.eq(DispatchOrder::getBillno, workOrderList.getWorkOrder());
//                                    List<DispatchOrder> dispatchOrders = dispatchOrderMapper.selectList(dispatchOrderLambdaQueryWrapper);
//                                    if (CollectionUtils.isNotEmpty(dispatchOrders)) {
//                                        DispatchOrder dispatchOrder = dispatchOrders.get(0);
//                                        String uuid = UUID.randomUUID().toString();
//                                        String checkno = new String("AFQ" + dateFormat.format(new Date()) + uuid.substring(uuid.length() - 4, uuid.length()));
//                                        String mcode = dispatchOrder.getMcode();
//                                        CheckmanAndWpno checkmanAndWpno = deviceMapper.selectCheckmanAndWpno(dispatchOrder.getBillno(), "JOB00145");
//                                        String checkman = "10528";
//                                        String mkerid = "10528";
//                                        String wpno = "CX290_014"; //gw
//                                        if (ObjectUtils.isNotEmpty(checkmanAndWpno)) {
//                                            checkman = checkmanAndWpno.getEmpno();
//                                            mkerid = checkmanAndWpno.getEmpno();
//                                            wpno = checkmanAndWpno.getWpno(); //gw
//                                        }
//
//                                        Date ddate = new Date();
//                                        Date mkerdate = new Date();
//                                        //1.来料检验	1  2.首检检验	2  3.成品检验	3  4.巡检检验	4  5.送检检验	5  99.条码冻结	99  出库检验	10  专检	11  12数采（抽真空（泰斯特））	12  121数采（冷媒灌注机）	121  123真空机（莱宝）	123  122安全性能测试仪	122  13品质设备点检	13  14卤检	14  15外观	15
//                                        Integer billtyp = 123; //真空机（莱宝）
//                                        String srcbillno = dispatchOrder.getBillno(); //pgd
//                                        Integer istates = 2; //2
//                                        String linesno = dispatchOrder.getLinesno(); //linesno
//                                        //String tm = splitArray2[4]; //tm
//
//                                        String dno = null;//文档编号
//                                        BigDecimal value = new BigDecimal(map.get("dataValues0")); //数采结果
//                                        String deviceno = device.getDeviceno(); //设备编号
//                                        Date numericdatatime = new Date();
//                                        try {
//                                            numericdatatime = dateFormat2.parse(map.get("testTime"));//时间
//                                        } catch (ParseException e) {
//                                            throw new RuntimeException(e);
//                                        }
//                                        QcCheckmaterial qcCheckmaterial = new QcCheckmaterial();
//                                        qcCheckmaterial.setCheckno(checkno);
//                                        qcCheckmaterial.setMcode(mcode);
//                                        qcCheckmaterial.setCheckman(checkman);
//                                        qcCheckmaterial.setMkerid(mkerid);
//                                        qcCheckmaterial.setDdate(ddate);
//                                        qcCheckmaterial.setMkerdate(mkerdate);
//                                        qcCheckmaterial.setBilltyp(billtyp);
//                                        qcCheckmaterial.setSrcbillno(srcbillno);
//                                        qcCheckmaterial.setIstates(istates);
//                                        qcCheckmaterial.setWpno(wpno);
//                                        qcCheckmaterial.setLinesno(linesno);
//                                        qcCheckmaterial.setTm(tm);
//                                        qcCheckmaterial.setChkresult(chkresult);
//                                        qcCheckmaterial.setDno(dno);
//                                        qcCheckmaterial.setValue(value);
//                                        qcCheckmaterial.setDeviceno(deviceno);
//                                        qcCheckmaterial.setNumericdatatime(numericdatatime);
//                                        qcCheckmaterial.setRemark1(map.get("stepIndex"));
//                                        qcCheckmaterialMapper.insert(qcCheckmaterial);
//                                        log.info("同步抽真空设备数据成功，设备{}，tm:{}", deviceno, tm);
//                                    } else {
//                                        log.error("无效派工单：{}", workOrderList.getWorkOrder());
//                                    }
//                                } else {
//                                    //log.error("无效条码：{}", tm);
//                                }
//                            } else {
//                                log.error("数据拆分异常：{}", result.get(0));
//                            }
//                        } else {
//                            //log.error("未获取到数据", result);
//                        }
//                    } else {
//                        //log.error("获取数据失败：{}", jsonObject);
//                    }
//                } catch (IOException e) {
//                    throw new RuntimeException(e);
//                }
//            }
//        } else {
//            log.error("未找到启动的设备！");
//        }
//    }
//
//    /**
//     * 定时同步设备一车间开机的抽真空设备数据，抽真空设备暂时只支持ZKJ-LB：真空机（莱宝D40C）.split("_")
//     */
//    private void syncAMA13001ZKJLBDataSplitBy_() {
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
//        SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
//        LambdaQueryWrapper<Device> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(Device::getDptno, "AMA13001")
//                .eq(Device::getDetypeno, "ZKJ-LB")
//                .eq(Device::getRunstates, 0);
//        List<Device> devices = deviceMapper.selectList(queryWrapper);
//        ////log.info(devices.toString());
//        if (CollectionUtils.isNotEmpty(devices)) {
//            for (Device device : devices) {
//                Map<String, Object> params = new HashMap<>();
//                params.put("deviceId", device.getDeviceno());
//                try {
//                    JSONObject jsonObject = restTemplateUtil.get("http://172.19.255.89:8083/zwz/deviceDataNew/getDeviceDataByDeviceBarIdInRecentDayOnScreen", params);
//                    //////log.info("response: {}",jsonObject);
//                    ////log.info(jsonObject.toJSONString());
//                    if (jsonObject.getBoolean("success")) {
//                        List<String> result = jsonObject.getObject("result", List.class);
//
//                        //String result = "2023-10-30 16:54:04##@@10100908000053202310300077##@@6539e1684cd6ca72d7c27b38_FALSE_{fd5a16ec-350d-4516-9fc9-c0697550f965}_2023/10/26 11:43:50_10100908000053202310300077_10300800200093_3_1_0_0_0_1_9.94643_0_0";
//                        //                        //        //{
//                        //                        //        //    "result": "2023-10-30 16:54:04##@@-1##@@001##@@0__0__0__0__0__0__0__0##@@0.00__0.00__0.00__0.00__0.00__0.00__0.00__0.00__0.00__0.00__0.00__0.00__0.00__0.00__0.00__0.00__0.00__0.00__0.00__0.00__0.00__0.00__0.00__0.00__0.00__0.00__0.00__0.00__0.00__0.00__0.00__0.00__0.00__0.00__0.00__0.00__0.00__0.00__0.00__0.00##@@5##@@5##@@",
//                        //                        //        //    "success": true,
//                        //        //    "code": 200,
//                        //        //    "timestamp": 1698658311546,
//                        //        //    "message": "OK"
//                        //}
//                        //时间##@@条码（空为-1##@@_id_isCleaned_guid_testTime_barcode_modelId_testNo_result_machine_stepIndex_dataResult_step_dataValues0
//                        //2023-10-30 16:54:04##@@10300800200093-231023-0065##@@6539e1684cd6ca72d7c27b38_FALSE_{fd5a16ec-350d-4516-9fc9-c0697550f965}_2023/10/26 11:43:50_10300800200093-231023-0065_10300800200093_3_1_0_0_0_1_9.94643_0_0
//                        //2023-10-30 16:54:04##@@-1##@@6539e1684cd6ca72d7c27b38_FALSE_{fd5a16ec-350d-4516-9fc9-c0697550f965}_2023/10/26 11:43:50_-1_3_1_0_0_0_1_9.94643_0_0
//                        //			                测试时间	     条码	设备号	结果1合格，2不合格		步骤号，顺序	结果0合格，1不合格	步骤1：开始，2准备	值，只有这个有数值，后续都是空
//                        //_id	isCleaned	guid	testTime	barcode	modelId	testNo	result	machine	stepIndex	dataResult	step	dataValues0
//                        if (result.size() > 0) {
//                            String[] splitArray1 = result.get(0).split("##@@");
//                            if (splitArray1.length >= 3) {
//                                ////log.info(splitArray1[2]);
//                                String[] splitArray2 = splitArray1[2].split("_");
//                                //////log.info("splitArray2: {}",Arrays.toString(splitArray2));//是否取消 FALSE TRUE
//
//                                if ("-1".equals(splitArray1[1])) {
//                                    //log.error("该设备{}无带条码数据",device.getDeviceno());
//                                    continue;
//                                }
//                                String tm = splitArray1[1]; //tm
//                                if (tm.indexOf("-") < 0) {
//                                    //10300800100018 231102 0418
//                                    ////log.info("条码：{}", tm);
//                                    tm = tm.substring(0, 14) + "-" + tm.substring(14, 20) + "-" + tm.substring(20, tm.length());
//                                }
//                                //判断数据是否已处理，已处理就不保存
//                                LambdaQueryWrapper<QcCheckmaterial> qcCheckmaterialLambdaQueryWrapper = new LambdaQueryWrapper<>();
//                                qcCheckmaterialLambdaQueryWrapper.eq(QcCheckmaterial::getTm, tm)
//                                        .eq(QcCheckmaterial::getDeviceno, device.getDeviceno());
//                                Long count = qcCheckmaterialMapper.selectCount(qcCheckmaterialLambdaQueryWrapper);
//                                if (count > 0) {
//                                    //log.debug("数据已处理");
//                                    continue;
//                                }
//                                //"AFQ202300000448"
//                                //INSERT INTO qc_checkmaterial (checkno,mcode,checkman,mkerid,ddate,mkerdate,billtyp,srcbillno,istates,wpno,linesno,tm,chkresult,dno,value,deviceno,numericdatatime)
//                                //VALUES (@billno,@mcode,@userno,@userno,getdate(),getdate(),@xjtype,@pgd,2,@gw,@linesno,@pc,@checkresult,@dno,@result,@deviceno,cast(@time as datetime))
//
//                                LambdaQueryWrapper<WorkOrderList> workOrderListLambdaQueryWrapper = new LambdaQueryWrapper<>();
//                                workOrderListLambdaQueryWrapper.eq(WorkOrderList::getBarcode, tm);
//                                List<WorkOrderList> workOrderLists = workOrderListMapper.selectList(workOrderListLambdaQueryWrapper);
//                                if (CollectionUtils.isNotEmpty(workOrderLists)) {
//                                    WorkOrderList workOrderList = workOrderLists.get(0);
//                                    LambdaQueryWrapper<DispatchOrder> dispatchOrderLambdaQueryWrapper = new LambdaQueryWrapper<>();
//                                    dispatchOrderLambdaQueryWrapper.eq(DispatchOrder::getBillno, workOrderList.getWorkOrder());
//                                    List<DispatchOrder> dispatchOrders = dispatchOrderMapper.selectList(dispatchOrderLambdaQueryWrapper);
//                                    if (CollectionUtils.isNotEmpty(dispatchOrders)) {
//                                        DispatchOrder dispatchOrder = dispatchOrders.get(0);
//                                        String checkno = new String("AFQ" + dateFormat.format(new Date()));
//                                        String mcode = dispatchOrder.getMcode();
//                                        CheckmanAndWpno checkmanAndWpno = deviceMapper.selectCheckmanAndWpno(dispatchOrder.getBillno(), "JOB00145");
//                                        String checkman = "10528";
//                                        String mkerid = "10528";
//                                        String wpno = "CX290_014"; //gw
//                                        if (ObjectUtils.isNotEmpty(checkmanAndWpno)) {
//                                            checkman = checkmanAndWpno.getEmpno();
//                                            mkerid = checkmanAndWpno.getEmpno();
//                                            wpno = checkmanAndWpno.getWpno(); //gw
//                                        }
//                                        Date ddate = new Date();
//                                        Date mkerdate = new Date();
//                                        //1.来料检验	1  2.首检检验	2  3.成品检验	3  4.巡检检验	4  5.送检检验	5  99.条码冻结	99  出库检验	10  专检	11  12数采（抽真空（泰斯特））	12  121数采（冷媒灌注机）	121  123真空机（莱宝）	123  122安全性能测试仪	122  13品质设备点检	13  14卤检	14  15外观	15
//                                        Integer billtyp = 123; //真空机（莱宝）
//                                        String srcbillno = dispatchOrder.getBillno(); //pgd
//                                        Integer istates = 2; //2
//                                        String linesno = dispatchOrder.getLinesno(); //linesno
//                                        //String tm = splitArray2[4]; //tm
//                                        Integer chkresult = Integer.parseInt(splitArray2[7]); //chkresult 0.合格	0  1.不合格	1
//                                        if (chkresult == 1) {
//                                            chkresult = 0;
//                                        } else {
//                                            chkresult = 1;
//                                        }
//                                        String dno = null;//文档编号
//                                        BigDecimal value = new BigDecimal(splitArray2[12]); //数采结果
//                                        String deviceno = device.getDeviceno(); //设备编号
//                                        Date numericdatatime = new Date();
//                                        try {
//                                            numericdatatime = dateFormat2.parse(splitArray2[3]);//时间
//                                        } catch (ParseException e) {
//                                            throw new RuntimeException(e);
//                                        }
//                                        QcCheckmaterial qcCheckmaterial = new QcCheckmaterial();
//                                        qcCheckmaterial.setCheckno(checkno);
//                                        qcCheckmaterial.setMcode(mcode);
//                                        qcCheckmaterial.setCheckman(checkman);
//                                        qcCheckmaterial.setMkerid(mkerid);
//                                        qcCheckmaterial.setDdate(ddate);
//                                        qcCheckmaterial.setMkerdate(mkerdate);
//                                        qcCheckmaterial.setBilltyp(billtyp);
//                                        qcCheckmaterial.setSrcbillno(srcbillno);
//                                        qcCheckmaterial.setIstates(istates);
//                                        qcCheckmaterial.setWpno(wpno);
//                                        qcCheckmaterial.setLinesno(linesno);
//                                        qcCheckmaterial.setTm(tm);
//                                        qcCheckmaterial.setChkresult(chkresult);
//                                        qcCheckmaterial.setDno(dno);
//                                        qcCheckmaterial.setValue(value);
//                                        qcCheckmaterial.setDeviceno(deviceno);
//                                        qcCheckmaterial.setNumericdatatime(numericdatatime);
//                                        qcCheckmaterialMapper.insert(qcCheckmaterial);
//                                    } else {
//                                        log.error("无效派工单：{}", workOrderList.getWorkOrder());
//                                    }
//                                } else {
//                                    //log.error("无效条码：{}", tm);
//                                }
//                            } else {
//                                log.error("数据拆分异常：{}", result.get(0));
//                            }
//                        } else {
//                            //log.error("未获取到数据", result);
//                        }
//                    } else {
//                        //log.error("获取数据失败：{}", jsonObject);
//                    }
//                } catch (IOException e) {
//                    throw new RuntimeException(e);
//                }
//            }
//        } else {
//            log.error("未找到启动的设备！");
//        }
//    }
}
